<?php namespace nimo\domains\acc\controllers;

use BaseController;
use Input;
use View;
use Validator;
use Redirect;
use Request;
use Response;

use nimo\domains\acc\models\Book as Book;

class BookEntryController extends BaseController {

    /**
     * Book Repository
     *
     * @var Book
     */
    protected $book;

    public function __construct(Book $book)
    {
        $this->book = $book;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $books = $this->book->all();
//        fwrite(fopen('php://stdout', 'w'), print_r($_SERVER,true));

        return View::make('nimo.domains.acc.main.entry', compact('books'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $params = array();
        return View::make("nimo.domains.acc.main.entry_w2ui", compact('params'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();
        $validation = Validator::make($input, Book::$rules);

        if ($validation->passes())
        {
            if (!is_null($book = $this->book->create($input))) {
                return Redirect::route('acc.book.show', $book->getJointPk())
                    ->with('success', 'There were successed.');
            }

            return Redirect::route('acc.book.index');
        }

        return Redirect::route('acc.book.create')
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $keys = explode('-', $id);
        $book = $this->book->where('domain', '=', 0)->where('ym', '=', $keys[0])->where('id', '=', $keys[1])->get();
        $l = Input::get('l');
        return View::make("nimo.domains.acc.main.{$l}entry_show", compact('book'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $book = $this->book->findByJointPk($id);

        if (is_null($book))
        {
            return Redirect::route('acc.book.index');
        }

        return View::make('nimo.domains.acc.book.edit', compact('book'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $input = array_except(Input::all(), '_method');
        $validation = Validator::make($input, Book::$rules);

        if ($validation->passes())
        {
            $book = $this->book->findByJointPk($id);
            $book->update($input);

            return Redirect::route('acc.book.show', $id)
                ->with('success', 'There were successed.');
        }

        return Redirect::route('acc.book.edit', $id)
            ->withInput()
            ->withErrors($validation)
            ->with('message', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $this->book->findByJointPk($id)->delete();

        return Redirect::route('acc.book.index');
    }

}